module EXECUTE(icode, ifun, icode_m, dstM_m, valM, valC, valA, valB, CC_i, valE, CC_o);
input [3:0] icode;
input [3:0] ifun;
input [3:0] icode_m;
input [3:0] dstM_m;
input [31:0] valM;
input [31:0] valC;
input [31:0] valA;
input [31:0] valB;
input [31:0] CC_i;
output reg [31:0] valE;
output reg [31:0] CC_o;

reg [31:0] aluA;
reg [31:0] aluB;
reg [3:0] aluOP;
reg [31:0] CC_t;

wire [31:0] aluOUT;
wire [2:0] aluFLAGS;


//MODULO da alu no estágio de execute
ALU a(.OP(aluOP), .A(aluA), .B(aluB), .C(aluOUT), .FLAGS(aluFLAGS));

always @(*) begin
	//Marretagem do POPF
	if(icode_m == 11 && icode != 6) begin
		if(dstM_m == 4'h8) begin
			CC_o <= valM;
		end else begin
			CC_o <= CC_t;
		end
	end else begin
		CC_o <= CC_t;
	end
end

always @(*) begin
    case(icode)
		default: begin
			CC_t <= CC_i;	
			aluA <= 32'bx;
			aluB <= 32'bx;
			aluOP <= 4'bx;
			valE <= 32'b0;
		end
		2: begin
			aluB <= 32'b0;
			aluA <= valA;
			aluOP <= 4'b0;
			valE <= aluOUT;
			CC_t <= CC_i;
		end
		3: begin
			aluB <= 32'b0;
			aluA <= valC;
			aluOP <= 4'b0;
			valE <= aluOUT;
			CC_t <= CC_i;
		end
		4, 5: begin
			aluB <= valB;
			aluA <= valC;
			aluOP <= 4'b0;
			valE <= aluOUT;
			CC_t <= CC_i;
		end
		6: begin
			aluB <= valB;
			aluA <= valA;
			aluOP <= ifun;
			valE <= aluOUT;
			CC_t[31:29] <= aluFLAGS;
			CC_t[28:0] <= CC_i[28:0];
		end
		8, 10: begin
			aluB <= valB;
			aluA <= 32'b100;
			aluOP <= 4'b1;
			valE <= aluOUT;
			CC_t <= CC_i;
		end
		9, 11: begin
			aluB <= valB;
			aluA <= 32'b100;
			aluOP <= 4'b0;
			valE <= aluOUT;
			CC_t <= CC_i;
		end
    endcase
end

endmodule
